﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SARD.Aux_Forms
{
    public partial class ROI : Form
    {

        int ret;
        List<Rectangle> rec;
        bool draw;
        Rectangle rectangle;

        /// <summary>
        /// Initializes the form.
        /// </summary>
        public ROI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes the form recieving a list of rois and an image.
        /// </summary>
        /// <param name="rect">
        /// It's a list of roi's
        /// </param>
        /// <param name="image">
        /// It's bmp image.
        /// </param>
        public ROI(List<Rectangle> rect, Image image)
        {
            InitializeComponent();
            rec = rect;
            draw = false;
            this.pictureBox1.Image = image;
            this.pictureBox1.Refresh();

            foreach (Rectangle re in rect)
                listBox1.Items.Add(re.ToString());

        }


        /// <summary>
        /// Sets the selected roi and closes the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            ret = listBox1.SelectedIndex;
            this.Close();

        }


        /// <summary>
        /// Cancel the selection, it is done by setting -1 to the selected index and closing the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ret = -1;
            this.Close();
        }


        /// <summary>
        /// Returns the selected index (after the form is closed).
        /// </summary>
        /// <returns>
        /// Returns an int number which is the selected roi.
        /// </returns>
        public int returnindex() 
        {
            return ret;
        }


        /// <summary>
        /// Updates the selected rectangle, and draw it in the image.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                rectangle = rec[listBox1.SelectedIndex];
                rectangle.Width = (int)(rectangle.Width * (float)this.pictureBox1.Width / this.pictureBox1.Image.Width);
                rectangle.Height = (int)(rectangle.Height * (float)this.pictureBox1.Height / this.pictureBox1.Image.Height);
                rectangle.X = (int)(rectangle.X * (float)this.pictureBox1.Width / this.pictureBox1.Image.Width);
                rectangle.Y = (int)(rectangle.Y * (float)this.pictureBox1.Height / this.pictureBox1.Image.Height);

                draw = true;
                pictureBox1.Refresh();
            }
        }


        /// <summary>
        /// Draw the rectangle.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (draw) 
            {
                //MessageBox.Show("Print: x: " + rectangle.X.ToString() + "y: " + rectangle.Y.ToString() + "w: " + rectangle.Width.ToString() + "h: " + rectangle.Height.ToString()); 
                Pen p = Pens.Red;
                e.Graphics.DrawRectangle(p, rectangle);
            }

            draw = false;
        }


    }
}
